package com.example.lin.ximalaya.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.lin.ximalaya.R;
import com.example.lin.ximalaya.pojo.Type1ListItem;

public class Type1PagerAdapter extends PagerAdapter {
    private Context context;
    private Type1ListItem type1ListItem;

    public Type1PagerAdapter(Context context) {
        this.context = context;
    }

    public Type1ListItem getType1ListItem() {
        return type1ListItem;
    }

    public void setType1ListItem(Type1ListItem type1ListItem) {
        this.type1ListItem = type1ListItem;
        //因为使用的 UltraViewPager,又因为UltraViewPager内部重新封装了ViewPager,所以该方法没有效果.
//        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return type1ListItem == null ? 0 : type1ListItem.getData().size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }


    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        Type1ListItem.DataBean dataBean = type1ListItem.getData().get(position);

        View layout = LayoutInflater.from(context).inflate(R.layout.view_rv_item_type_1_pager_item, container, false);

        ImageView iv_bannerImage = layout.findViewById(R.id.iv_bannerImage);

        Glide.with(context).load(dataBean.getCover()).into(iv_bannerImage);

        container.addView(layout);

        return layout;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

}
